
/*
* Copyright (c) 2013 Robin Lee.
*
* Authors: Robin Lee <honeycombs@263.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program;
*/

#include "appcmd.h"

#ifdef __cplusplus
extern "C"
{
# endif

char *appcmd_strim(char *s, const char *skip_str)
{
	char *e = s+strlen(s)-1;
	while(*s && strchr(skip_str,*s)) {*s++=0;};
	while(*e && e>s && strchr(skip_str,*e)) {*e=0;e--;};
	return s;
}

int appcmd_parse_cmdline (
	 char* pstr, const char* split,
	 char* argv[], int maxn
	)
{
	int argc, cc;
	size_t strl;
	char *sp, *st;

	if( (maxn<2) || (NULL == pstr) || (NULL == split) )
		return 0;

	strl = strlen(pstr);

	/* search */
	argc = 0;
	argv[0] = pstr;
	argv[1] = NULL;

	cc = 0;
	for(sp=pstr; ( (sp<pstr+strl) && argc<maxn); sp++)
	{
		for(st = (char*)split; *st; st++) {
			if( (*sp == *st) ) break;
		}
		if( !*st ) {
			cc++;
			continue;
		}

		*sp = 0;
		if(cc > 0) {
			cc=0;
			argc++;
			if(argc<maxn)
				argv[argc+1] = 0;
			else
				return argc;
		}
		argv[argc] = sp+1;
	}

	if( !*argv[argc] )
		argc--;

	return argc+1;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * option and command
 */
#define APPCMD_MAX_CMDCNT	32
#define APPCMD_MAX_CMDLEN	1024

typedef struct appcmd_cmdata_s
{
	int	argc;			// command count
	char *argv[APPCMD_MAX_CMDCNT];	// command and params
	char	cmd[APPCMD_MAX_CMDLEN];	// current command
}appcmd_cmdata_t;

int appcmd_options_runcmd(appcmd_options_t *opt_list, const char *fmt, ...)
{
	int argi, optn, rc = 0;

	va_list args;
	appcmd_options_t *poption;
	appcmd_cmdata_t *cmds = NULL, *subcmd = NULL;

	/* base check */
	if (!opt_list)
		return -EINVAL;

	/* alloc memory */
	cmds = (appcmd_cmdata_t*)malloc(sizeof(appcmd_cmdata_t));
	if( !cmds )
		return -ENOMEM;
	subcmd = (appcmd_cmdata_t*)malloc(sizeof(appcmd_cmdata_t));
	if( !subcmd ) {
		rc = -ENOMEM;
		goto safe_return;
	}
	memset(cmds, 0, sizeof(appcmd_cmdata_t));
	memset(subcmd, 0, sizeof(appcmd_cmdata_t));

	/* form commands */
	va_start(args,fmt);
    vsnprintf(cmds->cmd, APPCMD_MAX_CMDLEN, fmt, args);
	va_end(args);

	/*
	 * parse and polling multi command
	 */
	cmds->argc = appcmd_parse_cmdline(cmds->cmd, ";\r\n\0", cmds->argv, APPCMD_MAX_CMDCNT );
	if(cmds->argc<=0) {
		rc = -EINVAL;
		goto safe_return;
	}

	argi = 0;
polling_command:

	/* analyse command */
	strcpy(subcmd->cmd, cmds->argv[argi]);
	subcmd->argc = appcmd_parse_cmdline(subcmd->cmd, " \t\0", subcmd->argv, APPCMD_MAX_CMDCNT );
	if(subcmd->argc<=0) {
		goto safe_return;
	}

	/* run command */
	for( optn=0; (poption=&opt_list[optn]) && poption->name; optn++) {
		if( 0==strcmp(subcmd->argv[0], poption->name) )
			break;
		poption = NULL;
	};
	if( poption && poption->fun) {
		rc = poption->fun( subcmd->argc, subcmd->argv, poption->pri);
		if (rc) {
			printf("Error %d caused while process '%s'\n", rc, subcmd->cmd);
		}
	}

	argi++;
	if(argi<cmds->argc) {
		goto polling_command;
	}

	rc = 0;
safe_return:
	if (cmds)
		free(cmds);
	if (subcmd)
		free(subcmd);
	return rc;
}

#ifdef __cplusplus
}
# endif
